package kr.co.doriclassic.chapter24.student.service;

import java.util.Scanner;

import kr.co.doriclassic.chapter24.student.dao.InterfaceDao;

public abstract class ServiceItem<T> {
	protected InterfaceDao<T> dao;
	protected Scanner sc;
	
	public ServiceItem(InterfaceDao<T> dao, Scanner sc) {
		this.dao = dao;
		this.sc = sc;
		init();
		prnMenu();
	}

	public abstract T inputItemInfo(boolean gubun);
	public abstract void prnSubMenu();

	protected void init(){
		dao.loadFile();		
	}
	
	protected void close(){
		dao.saveFile();
	}
	
	protected void prnMenu(){
		int res=0;
		T item=null;
		do {
			System.out.print("메뉴[1.삽입 2.삭제 3.수정 4.검색 5.목록 6.종료]");
			res=sc.nextInt();
			switch (res) {
				case 1://추가
					item = inputItemInfo(true);
					dao.insertItem(item);
					break;
				case 2://삭제
					dao.prnItemList(null);
					System.out.println("삭제할 항목 입력");
					item = inputItemInfo(false);
					try {
						dao.deleteItem(item);
					} catch (ArrayIndexOutOfBoundsException e) {
						System.out.println("데이터 없음");
					}
					break;
				case 3://수정
					dao.prnItemList(null);
					System.out.println("수정할 항목 입력");
					item = inputItemInfo(true);
					dao.updateItem(item);
					break;
				case 4://검색
					System.out.println("검색할 항목 입력");
					item = inputItemInfo(false);
					dao.searchItem(item);
					break;
				case 5://목록
					prnSubMenu();
					break;
				default:
					close();
					res=7;
			}
		} while (res<=6);
	}	
}
